package com.sun.wanandroid.ui.main

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.sun.module_base.model.AppThemeState
import com.sun.module_common.route.RouteName
import com.sun.module_network.utils.logi

/**
 *@author:sunc
 *@time :2025/7/24 17:46
 *@desc: 描述
 */

@Composable
fun MainScreen(appThemeState: MutableState<AppThemeState>) {

    val navController = rememberNavController()

//    val items = listOf(
//        NavigationItem.Home, NavigationItem.Category, NavigationItem.Hot, NavigationItem.Mine
//    )

    val navBackStackEntry by navController.currentBackStackEntryAsState()
    //当前路由
    val currentDestination = navBackStackEntry?.destination
    val currentRoute = currentDestination?.route

    val modifier = Modifier
        .fillMaxSize()
        .statusBarsPadding()
        .navigationBarsPadding()

//    when (currentDestination?.route) {
//        RouteScreen.Home.route, RouteScreen.Hot.route, RouteScreen.Mine.route, RouteScreen.Books.route -> {
//            /**
//             * NavigationSuiteScaffold 支持3种类型导航栏，默认情况为NavigationBar
//             * 折叠屏和平板的导航类型叫NavigationRail
//             * 超大屏导航类型叫做NavigationDrawer
//             */
//            NavigationSuiteScaffold(
//                modifier = modifier,
//
//                navigationSuiteItems = {
//                    items.forEach { navItem ->
//                        item(
//                            icon = { Icon(navItem.icon, contentDescription = null) },
//                            label = { Text(navItem.title) },
//                            selected = currentDestination.hierarchy.any { it.route == navItem.route },
//                            onClick = {
//                                if (currentDestination.route != navItem.route) {
//                                    navController.navigate(navItem.route) {
//                                        popUpTo(navController.graph.findStartDestination().id) {
//                                            saveState = true
//                                        }
//                                        launchSingleTop = true
//                                        restoreState = true
//                                    }
//                                }
//                            })
//                    }
//                }
//
//            ) {
//                Navigation(navController = navController)
//            }
//        }
//
//        else -> {
//            Navigation(modifier = modifier, navController = navController)
//        }
//    }
    val tabs = listOf(
        RouteName.Home.route, RouteName.Hot.route, RouteName.Mine.route, RouteName.Category.route,
        RouteName.Square.route, RouteName.Questions.route, RouteName.Index.route
    )
    //需要添加观察key
    val showBar = remember (currentRoute){ derivedStateOf { currentRoute in tabs } }



    Scaffold(
        modifier = modifier,
        bottomBar = {
            AnimatedVisibility(
                visible = showBar.value,
                enter = slideInVertically(animationSpec = tween(200)) + fadeIn(),
                exit = slideOutVertically(animationSpec = tween(200)) + fadeOut(),
                modifier = Modifier.background(MaterialTheme.colorScheme.surface)
            ) {

//                when (currentDestination?.route) {
//                    RouteName.Home.route, RouteName.Hot.route, RouteName.Mine.route, RouteName.Category.route,
//                    RouteName.Square.route, RouteName.Questions.route, RouteName.Index.route ->
                BottomNavigationBar(navController = navController, currentRoute = currentRoute)
//                }
            }
        }
    ) {
        Navigation(modifier = Modifier.padding(it), navController = navController, appThemeState)
    }
}